package threadDemo;

/**
 * @author macpro
 * @date 2019-07-12
 * @function 结论：方法阻塞，会阻塞，调用其的 所有线程，线程整个wait 。。。
 */
public class WatchDogSerialMain {
    private int count=0;
    private String serialHeartThread= "serial";
    private static WatchDogSerialMain watchDogSerialMain;

    public static void main(String[] args) {
        WatchDogSeriaThread mWatchDogSerial = new WatchDogSeriaThread("serial", 1000 * 10, new WatchDogSeriaThread.WatchDogInterface() {
            @Override
            public void callNoThread(String str) {
                System.out.println("call back ...." +System.currentTimeMillis() +"  "+Thread.currentThread().getName());
                watchDogSerialMain.checkSerialConnect();
            }
        });
        mWatchDogSerial.start();
        watchDogSerialMain = new WatchDogSerialMain();
        watchDogSerialMain.serialHeartThread();
    }

    private  void serialHeartThread() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                while (true){
                    //判断串口链接 与否
                    try {
                        Thread.sleep(1000);
                        checkSerialConnect();
                    }catch (Exception e){
                        e.printStackTrace();
                    }
                }
            }
        }, serialHeartThread).start();
    }

    private void checkSerialConnect() {
        System.out.println("cc start" +System.currentTimeMillis()+"  "+Thread.currentThread().getName());
        if (count == 7 ){
            synchronized (serialHeartThread){
                try {
                    serialHeartThread.wait();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
        System.out.println("cccc "+count++);
    }
}
